<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/7/26
 * Time: 17:40
 */

namespace Api\Service;


class SheetCommentService
{
    /**
     * 添加评论
     */
    public function addComment($param)
    {
        $model = M('SheetComment');
        $res = $model->create($param);
        if ($res) {
            $param['user_id'] = $param['uid'];

            $id = $model->add($param);

            //曲谱评论数+1
            $sheetModel = M('Sheet')->where(array('id'=>$param['sheet_id']))->setInc('comment_count');

            return $id;
        } else {
            E($model->getError());
        }
    }

    /**
     * 获取曲谱评论内容
     */
    public function getComment($param)
    {
        $model = M('SheetComment');
        $sheetId = $param['sheet_id'];
        $map = array(
            'sheet_id' => $sheetId,
            'is_delete' => 0
        );
        $count = $model->where($map)->count();
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];
        $page = new PageService($param, $count, $listRows);
        $show = $page->show();
        $list = $model->where($map)
            ->order('create_time desc')->limit($page->firstRow.','.$page->listRows)
            ->select();

        //获取用户信息
        $userIds = array_column($list, 'user_id');
        $userService = new UserService();
        $userInfo = $userService->getMapByUids($userIds);
        //获取曲谱信息
        $sheetInfo = M('Sheet')->where(array('id'=>$sheetId))->field('id, name, singer, song_name')->select();
        $commentList['sheet'] = $sheetInfo[0];
        foreach ($list as $key => $val) {
            $commentList['list'][$key]['id'] = $val['id'];
            $commentList['list'][$key]['user'] = $userInfo[$val['user_id']];
            $commentList['list'][$key]['remark'] = $val['remark'];
            $commentList['list'][$key]['time'] = $val['create_time'];
        }
        if (empty($commentList['list'])) {
            $commentList['list'] = array();
        }
        $commentList['nowPage'] = $page->nowPage;
        $commentList['totalCount'] = $count;
        $commentList['totalPage'] = ceil($count / $listRows);
        return $commentList;
    }
}